package qianfeng.com.meijiadaka;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import qianfeng.com.meijiadaka.adapter.ImageAdapter;
import qianfeng.com.meijiadaka.adapter.OneDayAdapter;
import qianfeng.com.meijiadaka.bean.OneDayBean;
import qianfeng.com.meijiadaka.bean.TutorialBean;
import qianfeng.com.meijiadaka.interfacebean.OneDayData;
import qianfeng.com.meijiadaka.interfacebean.TutorialData;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class TutorialActivity extends AppCompatActivity {

    @BindView(R.id.find_ib)
    ImageButton mFindIb;
    @BindView(R.id.tutorial_sv)
    SearchView mTutorialSv;
    @BindView(R.id.tutorial_rrv)
    RefreshRecyclerView mRecyclerView;
    @BindView(R.id.activity_tutorial)
    LinearLayout mActivityTutorial;
    private String mPath = "http://home.naildaka.com/vapi/v4/home/tutorials";
    private String mUri = "http://home.naildaka.com/vapi/v4/home/tutorials/daily?pageNo=1";
    private ImageAdapter mAdapter;
    private ImageView mIv1;
    private ImageView mIv2;
    private ImageView mIv3;
    private ImageView mIv4;
    private RecyclerView mRecyclerView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tutorial);
        ButterKnife.bind(this);
        mAdapter = new ImageAdapter(this);
        View view = LayoutInflater.from(this).inflate(R.layout.header, mActivityTutorial,false);
        mIv1 = (ImageView) view.findViewById(R.id.header_iv1);
        mIv2 = (ImageView) view.findViewById(R.id.header_iv2);
        mIv3 = (ImageView) view.findViewById(R.id.header_iv3);
        mIv4 = (ImageView) view.findViewById(R.id.header_iv4);
        mIv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TutorialActivity.this, Tutorial_activityActivity.class);
                intent.putExtra("id",1);
                intent.putExtra("title","新手教程");
                startActivity(intent);
            }
        });
        mIv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TutorialActivity.this, Tutorial_activityActivity.class);
                intent.putExtra("id",2);
                intent.putExtra("title","开店秘籍");
                startActivity(intent);

            }
        });
        mIv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TutorialActivity.this, Tutorial_activityActivity.class);
                intent.putExtra("id",3);
                intent.putExtra("title","高手进阶");
                startActivity(intent);

            }
        });
        mIv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TutorialActivity.this, Tutorial_activityActivity.class);
                intent.putExtra("id",4);
                intent.putExtra("title","关睫教程");
                startActivity(intent);

            }
        });
        mAdapter.setHeader(view);
        View view1 = LayoutInflater.from(this).inflate(R.layout.image_tutorial, null);
        TextView tv1 = (TextView) view1.findViewById(R.id.image_tutorial_tv);
        TextView tv2 = (TextView) view1.findViewById(R.id.image_tutorial_title);
        mRecyclerView1 = (RecyclerView) view1.findViewById(R.id.image_tutorial_rv);
        tv2.setText("每日更新");
        mRecyclerView1.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        mAdapter.setFooter(view1);
        mRecyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                loadData1();
                loadData2();
            }
        });
        mRecyclerView.setLoadMoreAction(new Action() {
            @Override
            public void onAction() {

            }
        });
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.showSwipeRefresh();
                loadData1();
                loadData2();
            }
        });
    }

    private void loadData1() {
        mAdapter.clear();
        mRecyclerView.dismissSwipeRefresh();
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build().create(TutorialData.class).getData(mPath).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<TutorialBean>() {
            @Override
            public void call(TutorialBean tutorialBean) {
                mAdapter.clear();
                mAdapter.add(tutorialBean.getResult());
                mRecyclerView.dismissSwipeRefresh();

            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Toast.makeText(TutorialActivity.this, "网络不好", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadData2() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build().create(OneDayData.class).getData(mUri).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<OneDayBean>() {
            @Override
            public void call(OneDayBean oneDayBean) {
                List<OneDayBean.ResultBean> result = oneDayBean.getResult();
                Log.e("afaf","afafa");
                mRecyclerView1.setAdapter(new OneDayAdapter(TutorialActivity.this, result));

            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
            Log.e("afa","error");
            }
        });
    }

    @OnClick({R.id.find_ib, R.id.tutorial_sv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.find_ib:
                finish();
                break;
            case R.id.tutorial_sv:
                break;
        }
    }
}
